//index.js
const shikuang= require('../../config').shikuang;
const getCurrentPagesApi = require('../../utils/common').getCurrentPagesApi;
const host = require('../../config').host
//获取应用实例
const app = getApp()

var page = 0;
var end = true;
// 获取数据的方法，具体怎么获取列表数据大家自行发挥  
var GetList = function(that){  
  that.setData({  
    hidden:false  
  });
  // wx.showLoading({
  //   title : '加载中',
  //   mask : true
  // })
  end = false;
  var typeId = app.globalData.typeId;
  wx.request({  
    url:host+'api/shikuang/shikuang',  
    data:{  
      page : page,
      type2 : typeId
    },  
    success:function(res){
      // console.info(end);
      
      if(res.data != ""){
        var list = that.data.content;
        for(var i = 0; i < res.data.length; i++){
          list.push(res.data[i]);
        }
        that.setData({  
          content : list  
        });  
        page ++;
        end = true;
        that.setData({  
          hidden:true  
        });
      }else{
        end = false
        that.setData({  
          hiddens:true,
          hidden:true
        });
      }
    }  
  });  
}


Page({
  data: {
    content:[],
    showModal:true,
    hidden:true,
    hiddens:false,
    currentTypeId: 1,
    currentJiageId: 6,
    currentKucunId: 4,
    currentErjiId: 0,
    scrollTop: 0,
    scrollHeight: 0,
    typeList: [{'id':2,'tid':1,title:'实时价格'},
               {'id':3,'tid':2,title:'实时存库'},
               {'id':1,'tid':3,title:'市场解读'},],
    erjiList: [],

  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  toBankuai: function (event) {
    console.log(event);
    var typeTid = event.currentTarget.dataset.typetid;
    var typeId = event.currentTarget.dataset.typeid;
    app.globalData.typeId = event.currentTarget.dataset.typeid;
    var functions = event.currentTarget.dataset.function;
    if(functions == "shikuangs"){
      this.refresh();
      this.setData({
        currentErjiId:typeId,
      });
    }else if(functions == "jiage"){
      this.refresh();
      this.setData({
        currentJiageId:typeId,
      });
    }else if(functions == "kucun"){
      this.refresh();
      this.setData({
        currentKucunId:typeId,
      });
    }else if(functions == "shikuangTabss"){
      this.shikuangTabss(typeId);
      this.setData({
        currentTypeId:typeTid,
      });
    }
  },
  shikuangs: function(type2) {
    var that = this
    wx.request({
      url: host+'api/shikuang/shikuang',
      data: {'type2':type2},
      success: function(res){
        that.setData({
          content: res.data
        })
      },
      fail: function(data){
        console.log(data)
      }
    })
  },
  shikuangTabss: function(type,event) {
    console.log(this);
    // this.hideModal();
    var that = this
    wx.request({
      url: host+'api/shikuang/shikuangtabss',
      data: {'type':type},
      success: function(res){
        // console.log(res.data[0].id);
        // var type2 = res.data[0].id;
        if(type == 2){
          that.setData({
            erjiList: res.data,
            jiageModal: true,
            kucunModal: false,
            showModal: false
          }),
          app.globalData.typeId = res.data[0].id
          that.refresh();
        }else if(type == 3){
          that.setData({
            erjiList: res.data,
            jiageModal: false,
            kucunModal: true,
            showModal: false
          }),
          app.globalData.typeId = res.data[0].id
          that.refresh();
        }else if(type == 1){
          if(res.data == ""){
            that.setData({
              erjiList: res.data,
              jiageModal: false,
              kucunModal: false,
              showModal: false
            }),
            app.globalData.typeId = 0;
          that.refresh();
          }else{
            that.setData({
              erjiList: res.data,
              jiageModal: false,
              kucunModal: false,
              showModal: true
            })
          }
        }
        
      },
      fail: function(data){
        // console.log(data)
      }
    })
  },
  shikuang_detail: function(e) {
    console.log(e.currentTarget.dataset.id);
    wx.navigateTo({
      url: `./shikuang_detail?id=${e.currentTarget.dataset.id}`
    })
  },
  onShareAppMessage: function (res) {
    if (res.from === 'menu') {
      return {
        title: '国数源',
        path: getCurrentPagesApi()
      }
    }
    
  },
  onLoad: function () {
      // this.ready();
      this.shikuangTabss(2);
      var that = this;
       wx.getSystemInfo({  
       success:function(res){  
         // console.info(res.windowHeight);  
         that.setData({  
           scrollHeight:res.windowHeight  
         });  
       }  
     }); 
  },
  bindDownLoad:function(){  
    //  该方法绑定了页面滑动到底部的事件  
     var that = this;
     console.log(end);
     if(end){
      GetList(that);  
     }
   },
   // scroll:function(event){  
   //  //  该方法绑定了页面滚动时的事件，我这里记录了当前的position.y的值,为了请求数据之后把页面定位到这里来。  
   //   this.setData({  
   //     scrollTop : event.detail.scrollTop  
   //   });  
   // },  
   refresh:function(event){  
    //  该方法绑定了页面滑动到顶部的事件，然后做上拉刷新  
     page = 0;  
     this.setData({  
       content : [],  
       scrollTop : 0  
     });  
     GetList(this)  
   },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
